﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FullThrustLogic.Core;
using FullThrustLogic.Core.GameState;
using FullThrustLogic.Helper;
using FullThrustLogic.ShipComponent;
using FullThrustLogic.ShipComponent.Weapon;
using LogicTest.Forms;

namespace LogicTest.Controls
{
    public partial class AssignAttack : UserControl
    {
        private GameContext _gameContext;

        private PlayerShips _playerShips;
        private MapControl _mapControl;
        private ContextMenu _treeContextMenu;
        private MenuItem _assignAttackOrder;

        public AssignAttack(GameContext gameContext, MapControl mapControl)
        {
            InitializeComponent();

            _gameContext = gameContext;
            _mapControl = mapControl;

            _treeContextMenu = new ContextMenu();

            _assignAttackOrder = new MenuItem("Assign Attack Order");
            _treeContextMenu.MenuItems.Add(_assignAttackOrder);
            //_treeContextMenu.MenuItems.Add(_inspectShip);

            _assignAttackOrder.Click += new EventHandler(_assignMovementOrder_Click);
            


        }

        private void _assignMovementOrder_Click(object sender, EventArgs e)
        {
            AssignFCSWizard assignShipAttack = new AssignFCSWizard(_playerShips.SelectedShip, _gameContext);
            assignShipAttack.Closed += new EventHandler(assignShipAttack_Closed);
            assignShipAttack.ShowDialog();
        }

        void assignShipAttack_Closed(object sender, EventArgs e)
        {
            if (_playerShips.SelectedShip != null)
            {
                _playerShips.SelectedShip.ApplyAttackOrders();
                _playerShips.UpdateSelectedShip();
                ((AttackOrderState)_gameContext.CurrentState).GetNextPlayer();
                LoadAttackingPlayer();
            }
            else
            {
                MessageBox.Show("You must select an attacking ship");
            }
        }

        private void AssignAttack_Load(object sender, EventArgs e)
        {
            LoadAttackingPlayer();

        }

        private void LoadContextMenuIntoShipTreeNodes(TreeNode ParentNode)
        {
            ParentNode.ToolTipText = "Right click ship to assign attack order";
            
            if (ParentNode.Tag is Ship)
            {
                ParentNode.ContextMenu = _treeContextMenu;
            }

            foreach (TreeNode node in ParentNode.Nodes)
            {
                LoadContextMenuIntoShipTreeNodes(node);
            }
        }

        private void LoadAttackingPlayer()
        {
            if (_gameContext.CurrentState is AttackOrderState)
            {
                pnlPlayerShips.Controls.Clear();

                _playerShips = new PlayerShips(_gameContext.Players[(int)((AttackOrderState)_gameContext.CurrentState).ControllingPlayer].AvailableToAttackShips){Dock = DockStyle.Fill};
                pnlPlayerShips.Controls.Add(_playerShips);

                _playerShips.tvPlayerShips.ShowNodeToolTips = true;
                foreach (TreeNode node in _playerShips.tvPlayerShips.Nodes)
                {
                    LoadContextMenuIntoShipTreeNodes(node);
                }

                label1.Text = ((AttackOrderState) _gameContext.CurrentState).ControllingPlayer.ToString();
            }
        }
    }
}
